#include "GlobalInstances.h"

extern _Globals *Globals = NULL;
extern _Random *Random = NULL;

_Globals::_Globals( void )
{
	Player = new TestShip(gd3dDevice, 2000, D3DXVECTOR3(1,1,1));
	Player->SetPosition(D3DXVECTOR3(0, 0, 0));

	Player->InitThrusters(1800);
	Player->BoundingBox.InitCollisionBox(1,1,1);

	SpaceBox = new Skybox(gd3dDevice);
	SpaceBox->LoadTextureFromFile("Textures/SpaceSkybox2.bmp");

	Missile = new BaseMissile(gd3dDevice, Player->GetPosition(), D3DXVECTOR3(0,0,0),
							  800.f, 200.f, 200.f, 150.f, 5.5f);
	Missile->CreateCube(D3DXVECTOR3(0.2f, 0.2f, 2.f));
	Missile->InitCollisionBox(D3DXVECTOR3(0.2f, 0.2f, 2.f));
}

_Globals::~_Globals( )
{
	for(UINT i = 0; i < Planets.size(); ++i)
	{
		delete Planets[i];
		Planets[i] = NULL;
	}

	for(UINT i = 0; i < Bots.size(); ++i)
	{
		delete Bots[i];
		Bots[i] = NULL;
	}

	if(Player != NULL)
	{
		delete Player;
		Player = NULL;
	}

	if(SpaceBox != NULL)
	{
		delete SpaceBox;
		SpaceBox = NULL;
	}

	if(Missile != NULL)
	{
		delete Missile;
		Missile = NULL;
	}

	for(UINT i = 0; i < GameTextures.size(); ++i)
	{
		delete GameTextures[i];
		GameTextures[i] = NULL;
	}

	for(UINT i = 0; i < GameModels.size(); ++i)
	{
		delete GameModels[i];
		GameModels[i] = NULL;
	}
}

void _Globals::AddPlanet(IDirect3DDevice9 *nDevice, D3DXVECTOR3 nPosition, D3DXVECTOR3 nScale,
                          D3DXVECTOR3 RotationSpeed, float nRadius, std::string Filename)
{
	Planet *TempPlanet = NULL;

	TempPlanet = new Planet(nDevice, nPosition, nScale, RotationSpeed);

	TempPlanet->SetCollisionSphere(nRadius);
	TempPlanet->LoadXFile(Filename, 0);
	Planets.push_back(TempPlanet);

}

void _Globals::AddBot(IDirect3DDevice9 *nDevice, float nMass, State *nState, D3DXVECTOR3 BotSize,
		          float nThrust)
{
	Bot *TempBot = NULL;

	TempBot = new Bot(nDevice, nMass, nState, BotSize, nThrust);
	Bots.push_back(TempBot);

}

void _Globals::Update(float dt)
{
	UINT i = 0, LoopSize = 0;

	for( i = 0, LoopSize = Planets.size(); i < LoopSize; ++i)
	{
		Planets[i]->Update(dt);
	}

	for( i = 0, LoopSize = Bots.size(); i < LoopSize; ++i)
	{
		Bots[i]->Update(dt);
	}

	Player->Update(dt);


	if(!Missile->IsActive() && !Missile->GetOldActive())
	{
		Missile->SetAngles(Player->GetAngles());

		D3DXVECTOR3 RotVec(1.5f, 0, 0);
		D3DXMatrixRotationYawPitchRoll(&M1, Missile->GetAngles().x, Missile->GetAngles().y, Missile->GetAngles().z);

		D3DXVec3TransformCoord(&RotVec, &RotVec, &M1);

		Missile->SetPosition(Player->GetPosition() + RotVec);
	}
	
	Missile->Update(dt);
	SpaceBox->SetPosition(Player->GetPosition());
	SpaceBox->Update(dt);

}

bool _Globals::CheckSpawnCollision(Planet *P1, Planet *P2)
{
	float d = D3DXVec3LengthSq(&(P2->GetPosition() - P1->GetPosition()));

	if(d <= ((P1->MainSphere.Radius * P1->MainSphere.Radius) + (P2->MainSphere.Radius * P2->MainSphere.Radius)))
		return true;
	else
		return false;
}

void _Globals::CleanUpModelList( void )
{
	for(UINT i = 0; i < GameModels.size(); ++i)
	{
		if(GameModels[i]->MaterialBuffer != NULL)
		{
			GameModels[i]->MaterialBuffer->Release();
			GameModels[i]->MaterialBuffer = NULL;
		}
	}
}